package com.jjb.www.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextClock;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.permissions.Permission;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.shape.view.ShapeImageView;
import com.hjq.shape.view.ShapeTextView;
import com.hjq.widget.layout.WrapRecyclerView;
import com.jjb.www.R;
import com.jjb.www.aop.Permissions;
import com.jjb.www.app.AppFragment;
import com.jjb.www.http.api.AgencyListApi;
import com.jjb.www.http.api.GetPlanStudentInfoApi;
import com.jjb.www.http.api.GetStudentPlanDetailApi;
import com.jjb.www.http.api.GetTrainingSdkScoreApi;
import com.jjb.www.http.api.PrePostPlanVideoRecordApi;
import com.jjb.www.http.api.UserLiveInfoApi;
import com.jjb.www.http.api.WatchPlaybackApi;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.other.AppConfig;
import com.jjb.www.ui.activity.BrowserActivity;
import com.jjb.www.ui.activity.CopyActivity;
import com.jjb.www.ui.activity.PunchClockRecordActivity;
import com.jjb.www.ui.activity.VerificationRecordActivity;
import com.jjb.www.ui.adapter.StudentPlan1Adapter;
import com.jjb.www.ui.adapter.UserLiveInfoAdapter;
import com.jjb.www.utils.StringBufferUtils;
import com.jjb.www.widget.StripeProgressBar;

import java.util.ArrayList;
import java.util.List;

/**
 * author : Android CRG
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 可进行拷贝的副本
 */
public final class StudyListJiNengFragment extends AppFragment<CopyActivity> implements BaseAdapter.OnChildClickListener {
    private static final String INTENT_KEY_IN_PLAN_TYPE = "planType";
    private static final String INTENT_KEY_IN_TYPE = "type";
    private static final String INTENT_KEY_IN_ID = "student_id";
    private ShapeTextView mTvTotalScore;
    private ShapeTextView mTvAlreadyTotalScore;
    private ShapeLinearLayout mLlDaka;
    private FrameLayout mFlPunch;
    private AppCompatImageView mIvPunchBg;
    private TextView mTvPunchType;
    private TextClock mTcTime;
    private ShapeLinearLayout mLlKcap;
    private ShapeLinearLayout mLlXsjl;
    private WrapRecyclerView mRvDkjlList;
    private ShapeLinearLayout mLlKcb;
    private ShapeTextView mTvTost;
    private ShapeTextView mTvBtn;
    private WrapRecyclerView mRvCkbList;
    private ShapeTextView mTvName;
    private ShapeTextView mTvSchoolName;
    private ShapeTextView mTvSchoolAddress;
    private ShapeTextView mTvTime;
    private ShapeTextView mTvClassNum;
    private ShapeTextView mTvTaskNum;

    private StudentPlan1Adapter plan1Adapter;
    private UserLiveInfoAdapter liveInfoAdapter;

    //3岗前/2继教
    private String type="3";
    //1线上 2线下 4 混合
    private String planType;
    //岗位id
    private String student_id;
    private GetTrainingSdkScoreApi.Bean sdkInfoDTO;
    private GetPlanStudentInfoApi.Bean infoBean;
    private ShapeTextView mTvPunchClock;
    private ShapeImageView mImgPhone;





    public static StudyListJiNengFragment newInstance(String student_id, String type, String planType) {
        StudyListJiNengFragment fragment = new StudyListJiNengFragment();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_KEY_IN_PLAN_TYPE, planType);
        bundle.putString(INTENT_KEY_IN_TYPE, type);
        bundle.putString(INTENT_KEY_IN_ID, student_id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_study_list_ji_neng;
    }

    @Override
    protected void initView() {
        mTvClassNum = findViewById(R.id.tv_class_num);
        mTvTaskNum = findViewById(R.id.tv_task_num);
        mRvCkbList = findViewById(R.id.rv_ckb_list);
        mTvTotalScore = findViewById(R.id.tv_total_score);
        mTvAlreadyTotalScore = findViewById(R.id.tv_already_total_score);
        mLlDaka = findViewById(R.id.ll_daka);
        mImgPhone = findViewById(R.id.img_phone);
        mTvPunchClock = findViewById(R.id.tv_punch_clock);
        mFlPunch = findViewById(R.id.fl_punch);
        mTvTime = findViewById(R.id.tv_time);
        mIvPunchBg = findViewById(R.id.iv_punch_bg);
        mTvPunchType = findViewById(R.id.tv_punch_type);
        mTcTime = findViewById(R.id.tc_time);
        mLlKcap = findViewById(R.id.ll_kcap);
        mLlXsjl = findViewById(R.id.ll_xsjl);
        mRvDkjlList = findViewById(R.id.rv_dkjl_list);
        mLlKcb = findViewById(R.id.ll_kcb);
        mTvTost = findViewById(R.id.tv_tost);
        mTvBtn = findViewById(R.id.tv_btn);
        mTvName = findViewById(R.id.tv_name);
        mTvSchoolName = findViewById(R.id.tv_school_name);
        mTvSchoolAddress = findViewById(R.id.tv_school_address);
        liveInfoAdapter = new UserLiveInfoAdapter(getContext());
        liveInfoAdapter.setOnChildClickListener(R.id.tv_btn,this);
        mRvCkbList.setAdapter(liveInfoAdapter);
        mRvCkbList.setLayoutManager(new LinearLayoutManager(getContext()));

        plan1Adapter = new StudentPlan1Adapter(getContext());
        mRvDkjlList.setAdapter(plan1Adapter);
        mRvDkjlList.setLayoutManager(new LinearLayoutManager(getContext()));

        setOnClickListener(mLlXsjl,mTvPunchClock,mImgPhone);

    }

    @Override
    public void onClick(View view) {
        if (view==mLlXsjl){
            Intent intent = new Intent();
            intent.putExtra("id_card",sdkInfoDTO.getIdCard()+"");
            intent.putExtra("planCode",sdkInfoDTO.getPlanCode()+"");
            intent.putExtra("student_id",student_id+"");
            intent.putExtra("trainPostCode",sdkInfoDTO.getTrainPostCode()+"");
            intent.setClass(getContext(),VerificationRecordActivity.class);
            startActivity(intent);
        }else if (view==mTvPunchClock){
            Intent intent = new Intent();
            intent.putExtra("mech_id","");
            intent.putExtra("mech_training_sentry_id","");
            intent.putExtra("student_id",student_id+"");
            intent.putExtra("mech_training_plan_id",infoBean.getMech_teach_plan_id()+"");
            intent.setClass(getContext(), PunchClockRecordActivity.class);
            startActivity(intent);
        }else if (view==mImgPhone){
            callPhone(infoBean.getMobile());
        }
    }
    @Permissions({Permission.CALL_PHONE})
    private void callPhone(String phone) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        startActivity(intent);
    }
    @Override
    protected void initData() {

        planType = getString(INTENT_KEY_IN_PLAN_TYPE);
        student_id = getString(INTENT_KEY_IN_ID);
        GetPlanStudentInfo(type, student_id);
        //1线上 2线下 4 混合
        if ("1".equals(planType)) {
            mLlDaka.setVisibility(View.VISIBLE);
        } else if ("2".equals(planType)) {
            mLlXsjl.setVisibility(View.VISIBLE);
            mLlKcb.setVisibility(View.VISIBLE);
            mTvTost.setVisibility(View.VISIBLE);
            mTvBtn.setVisibility(View.VISIBLE);
            getUserLiveInfo();
            getTrainingSdkScore();
        } else if ("4".equals(planType)) {
            mLlKcap.setVisibility(View.VISIBLE);
            mLlXsjl.setVisibility(View.VISIBLE);
            mRvDkjlList.setVisibility(View.VISIBLE);
            getStudentPlanDetail();
        }
    }

    private void getStudentPlanDetail() {
        EasyHttp.get(this)
                .api(new GetStudentPlanDetailApi()
                        .setStudent_id(student_id)
                )
                .request(new HttpCallback<HttpData<GetStudentPlanDetailApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<GetStudentPlanDetailApi.Bean> data) {
                        mTvClassNum.setText(data.getData().getInfo().getClass_num()+"");
                        mTvTaskNum.setText(data.getData().getInfo().getTask_num()+"");
                        plan1Adapter.setData(data.getData().getList());
                    }

                });
    }

    /**
     * sdk用的
     */
    private void getTrainingSdkScore() {
        EasyHttp.get(this)
                .api(new GetTrainingSdkScoreApi()
                        .setType(planType + "")
                        .setStudent_id(student_id)
                        )
                .request(new HttpCallback<HttpData<GetTrainingSdkScoreApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<GetTrainingSdkScoreApi.Bean> data) {
                        sdkInfoDTO = data.getData();
                    }

                });
    }

    /**
     * 获取直播课程表
     */
    private void getUserLiveInfo() {
        EasyHttp.get(this)
                .api(new UserLiveInfoApi()
                        .setType(planType+ "")
                        .setId(student_id)
                        .setMinType(type)
                        .setToken(AppConfig.getInstance().getToken()))
                .request(new HttpCallback<HttpData<UserLiveInfoApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<UserLiveInfoApi.Bean> data) {
//                        liveInfoAdapter.setData(data.getData().getTime_list());
                        List<String> list = new ArrayList<>();
                        list.clear();
                        for (UserLiveInfoApi.Bean.TimeListDTO listImg :data.getData().getTime_list()){
                            list.add(listImg.getId()+"");
                        }
                        String ids = StringBufferUtils.arrToStr(list,",");
                        getVideoRecord(data.getData().getTime_list(),data.getData().getTeach_plan_info().getTeach_plan_id()+"",ids);
                    }

                });
    }

    private void getVideoRecord(List<UserLiveInfoApi.Bean.TimeListDTO> list,String planId,String idsList) {
        EasyHttp.get(this)
                .api(new PrePostPlanVideoRecordApi()
                        .setType("2")
                        .setToken(AppConfig.getInstance().getToken())
                        .setPlan_id(planId)
                .setTask_ids(idsList))
                .request(new HttpCallback<HttpData<PrePostPlanVideoRecordApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<PrePostPlanVideoRecordApi.Bean> data) {
                        mTvTime.setText(data.getData().getOtherInfo());
                        for (UserLiveInfoApi.Bean.TimeListDTO list:list) {
                            for (PrePostPlanVideoRecordApi.Bean.ListDTO list2:data.getData().getList()) {
                                if (list.getId().equals(list2.getTask_id())){
                                    list.setGenerate(true);
                                    list.setChannel_plan_id(list2.getId());
                                }
                            }
                        }
                        liveInfoAdapter.setData(list);
                    }

                });
    }

    private void GetPlanStudentInfo(String type, String student_id) {
        EasyHttp.get(this)
                .api(new GetPlanStudentInfoApi()
                        .setType(type + "")
                        .setStudent_id(student_id))
                .request(new HttpCallback<HttpData<GetPlanStudentInfoApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<GetPlanStudentInfoApi.Bean> data) {
                        infoBean = data.getData();
                        mTvName.setText(data.getData().getSentry_name());
                        mTvSchoolName.setText(data.getData().getMech_name());
                        mTvSchoolAddress.setText(data.getData().getSchool_address());
                        mTvTotalScore.setText(data.getData().getXu_total_score()+"");
                        mTvAlreadyTotalScore.setText(data.getData().getClass_num()+"");
                    }

                });
    }

    @Override
    public void onChildClick(RecyclerView recyclerView, View childView, int position) {
        if (liveInfoAdapter.getItem(position).isGenerate()){
            EasyHttp.get(this)
                    .api(new WatchPlaybackApi()
                    .setToken(AppConfig.getInstance().getToken())
                    .setChannel_plan_id(liveInfoAdapter.getItem(position).getChannel_plan_id()+"")
                    .setMech_id(liveInfoAdapter.getItem(position).getMech_id()+""))
                    .request(new HttpCallback<HttpData<String>>(this) {

                        @Override
                        public void onSucceed(HttpData<String> data) {
                            BrowserActivity.start(getContext(),data.getData());
                        }
                    });
        }
    }
}